/*
 * GuiUtils.java
 * Created on July 25, 2007, 11:16 AM
 */

package view;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author erol
 */
public class GuiUtils {
    
    public static void setColor(Graphics g, Color c){
        g.setColor(c.getR(), c.getG(), c.getB());
    }
    
    public static Color getColor(String color){
        return null;
    }
    
    public static Color getCurrentColor(Graphics g){
        
        return new Color(g.getRedComponent(), 
                        g.getGreenComponent(),
                        g.getBlueComponent());
    }
    
    /**
     *  @deprecated  
     */
    public static void drawRectangle(Graphics g, 
                                int x, int y, int width, int height, 
                                Color softLineColor, Color hardLineColor){
        
        GuiUtils.setColor(g, hardLineColor);
        g.drawLine(x, y, x, y + height - 1); // first vertical
        g.drawLine(x, y, x + width - 1, y); // first horizontal
        
        GuiUtils.setColor(g, softLineColor);
        g.drawLine(x + width - 1, y, x + width - 1, y + height - 1); // second vertical
        g.drawLine(x, y + height - 1, x + width - 1, y + height - 1); // second horizontal
    }
    
    public static void drawRectangle(Graphics g, 
                                int x, int y, int width, int height, 
                                Color softLineColor, Color hardLineColor, 
                                int tickness){
        
        GuiUtils.setColor(g, hardLineColor);
        g.fillRect(x, y, tickness, height); // first vertical        
        g.fillRect(x, y, width, tickness); // first horizontal
        
        GuiUtils.setColor(g, softLineColor);
        g.fillRect(x + width - tickness, y, tickness, height); // second vertical        
        g.fillRect(x, y + height - tickness, width, tickness); // second horizontal
    }
    
    public static void drawCircle(Graphics g,
                                int x, int y, int diameter, int tickness,
                                Color color){
        
        GuiUtils.setColor(g, color);
        
        for(int i = 1; i <= tickness; i++){
            g.drawArc(x+i-1, y+i-1, diameter - (i-1)*2, diameter - (i-1)*2, 0, 360);
        }                
    }
    
    /**
     *  diameter:13, tickness:4 
     *  GuiUtils.drawCircle(g, 10, 10, 13, 4, Color.BLACK, Color.WHITE);
     * 
     *  @return radius
     */
    public static int drawCircle(Graphics g,
                                int x, int y, int diameter, int tickness,
                                Color lineColor, Color bgColor){
                                        
        GuiUtils.fillCircle(g, x, y, diameter, lineColor);        
        GuiUtils.fillCircle(g, x+tickness-1, y+tickness-1, diameter - (tickness-1)*2, bgColor);
        
        return (diameter/2) + (diameter%2);
    }
    
    public static void drawCircleFromCenter(Graphics g,
                                int x, int y, int radius, int tickness,
                                Color lineColor, Color bgColor){        
        x = x - radius + 1;
        y = y - radius + 1;
        
        GuiUtils.drawCircle(g, x, y, radius * 2 - 1, tickness, lineColor, bgColor);
    }
    
    public static void fillCircle(Graphics g,
                                int x, int y, int diameter,
                                Color color){
                                
        GuiUtils.setColor(g, color);
        g.fillArc(x, y, diameter, diameter, 0, 360);
    }
    
    public static void fillCircleFromCenter(Graphics g,
                                int x, int y, int radius,
                                Color color){
        x = x - radius + 1;
        y = y - radius + 1;
        
        fillCircle(g, x, y, radius * 2 - 1, color);
    }
    
    /**
     *  GuiUtils.drawCircleFromCenter(g, 70, 70, 7, 4, Color.BLACK, Color.WHITE);
     *  GuiUtils.fillCircleFromCenter(g, 70, 70, 3, Color.BLACK);                 
     */
    
    //---------------------------------------------------------
    
    /**
     *  draws from center <br>
     * 
     *  @param oll outer circle radius
     *  @param olt outer circle line tickness
     *  @param olc outer circle color
     *  @param ill inner circle radius
     *  @param ilc inner circle color
     *  @param outerLineExists true if an outer circle will be drawn
     *  @param ot outer line tickness
     *  @param oc outer line color
     *  <br> <br>
     *  GuiUtils.drawRadioButton(g, 20, 200, 13, 4, Color.BLUE, 
     *                           7, Color.BLACK, true, true, 1, Color.RED, Color.WHITE);  <br>  <br>
     *   
     *   GuiUtils.drawRadioButton(g, 20, 260, 9, 3, Color.BLUE, 
     *                           4, Color.BLACK, true, true, 1, Color.RED, Color.WHITE);  <br> <br>
     *   
     *   GuiUtils.drawRadioButton(g, 60, 260, 7, 2, Color.BLUE, 
     *                           4, Color.BLACK, true, true, 1, Color.RED, Color.WHITE);  <br> <br>
     *   
     *   GuiUtils.drawRadioButton(g, 85, 260, 7, 3, Color.BLUE, 
     *                           4, Color.BLACK, true, true, 1, Color.RED, Color.WHITE);  <br> <br>
     */
    public static void drawRadioButton(Graphics g, int x, int y,
                                    int oll, int olt, Color olc,
                                    int ill, Color ilc,
                                    boolean selected, 
                                    boolean outerLineExists,
                                    int ot, Color oc,
                                    Color bgColor) {        
        if(outerLineExists){
            
            int radius = oll + ot;
            GuiUtils.drawCircleFromCenter(g, x, y, radius, ot, oc, bgColor);                        
        }
        
        GuiUtils.drawCircleFromCenter(g, x, y, oll, olt, olc, bgColor);
        
        if(selected){
            
            GuiUtils.fillCircleFromCenter(g, x, y, ill, ilc);
        }
        
    }
    
    /**
     *  does not draw from center <br>
     * 
     *  @param oll outer line length
     *  @param ot outer line tickness
     *  @param oc outer line color
     *  @param ill inner line length
     *  @param ilc inner line color
     *  @param outerLineExists 
     *  @param outerLineExists true if an outer circle will be drawn
     *  @param ot outer line tickness
     *  @param oc outer line color
     */
    public static void drawCheckboxButton(Graphics g, int x, int y,
                                    int oll, int olt, Color olc,
                                    int ill, Color ilc,
                                    boolean selected,
                                    boolean outerLineExists,
                                    int ot, Color oc,
                                    Color bgColor){
        if(outerLineExists){
            
            int width = oll + 2*ot;
            GuiUtils.drawRectangle(g, x, y, width, width, oc, oc, ot);
        }        
        
        int xtmp = x + ot;
        int ytmp = y + ot;
        GuiUtils.setColor(g, bgColor);
        g.fillRect(xtmp, ytmp, oll, oll);
        GuiUtils.drawRectangle(g, xtmp, ytmp, oll, oll, olc, olc, olt);                
        
        if(selected){
            
            xtmp = x + oll/2 - ill/2 + ot;
            ytmp = y + oll/2 - ill/2 + ot;
            GuiUtils.fillRect(g, xtmp, ytmp, ill, ill, ilc);            
        }
    }
    
    public static void fillRect(Graphics g, int x, int y, int width, int height, Color color){
    
        GuiUtils.setColor(g, color);
        g.fillRect(x, y, width, height);        
    }
    
}
